package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/20 15:43
 */
public class CompanyEventsQueryFragment extends BaseFragment {
  protected static final String EXTRA_CONTENT =
      CompanyEventsQueryActivity.class.getSimpleName() + "EXTRA_CONTENT";
  @BindView(R.id.tv_beginoccurrenceTime) TextView tvBeginoccurrenceTime;
  @BindView(R.id.ib_beginoccurrenceTime) ImageView ibBeginoccurrenceTime;
  @BindView(R.id.tv_endoccurrenceTime) TextView tvEndoccurrenceTime;
  @BindView(R.id.ib_endoccurrenceTime) ImageView ibEndoccurrenceTime;
  @BindView(R.id.tv_eventCategory) TextView tvEventCategory;
  @BindView(R.id.ib_eventCategory) ImageView ibEventCategory;
  @BindView(R.id.tv_chargePerson) TextView tvChargePerson;
  @BindView(R.id.ib_chargePerson) ImageView ibChargePerson;

  private String beginoccurrenceTime;
  private String endoccurrenceTime;
  private String chargeCode;
  private String chargeName;
  private String eventCategoryCode;
  private String eventCategoryName;
  private final int BEGINREG_DATE = 0;
  private final int ENDREG_DATE = 1;
  private final int EVENT_CATEGORY = 2;
  private final int CHARGE_PERSON = 3;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
    ExtraValueContent mContent = (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    beginoccurrenceTime = mContent.getExtName1();
    endoccurrenceTime = mContent.getExtName2();
    eventCategoryName = mContent.getExtName3();
    eventCategoryCode = mContent.getExtCode3();
    chargeName = mContent.getExtName4();
    chargeCode = mContent.getExtCode4();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(beginoccurrenceTime);
        queryConten.setExtName2(endoccurrenceTime);
        queryConten.setExtName3(eventCategoryName);
        queryConten.setExtCode3(eventCategoryCode);
        queryConten.setExtName4(chargeName);
        queryConten.setExtCode4(chargeCode);
        Intent intent = new Intent();
        intent.putExtra(EXTRA_CONTENT, queryConten);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_company_event_query, container, false);
    ButterKnife.bind(this, view);
    bindData();
    return view;
  }

  public void bindData() {
    tvBeginoccurrenceTime.setText(beginoccurrenceTime);
    tvEndoccurrenceTime.setText(endoccurrenceTime);
    tvEventCategory.setText(eventCategoryName);
    tvChargePerson.setText(chargeName);
  }

  @OnClick({
      R.id.ib_beginoccurrenceTime, R.id.ib_endoccurrenceTime, R.id.ib_eventCategory,
      R.id.ib_chargePerson
  }) public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_beginoccurrenceTime:
        showDialog(BEGINREG_DATE);
        break;
      case R.id.ib_endoccurrenceTime:
        showDialog(ENDREG_DATE);
        break;
      case R.id.ib_eventCategory:
        showDialog(EVENT_CATEGORY);
        break;
      case R.id.ib_chargePerson:
        //showDialog(CHARGE_PERSON);

        Intent chargePerson = new Intent(getActivity(), SearchPersonActivity.class);
        chargePerson.putExtra(SearchPersonListFragment.EXTRA_FROM,
            SearchPersonListFragment.VALUE_COMMON);
        startActivityForResult(chargePerson, CHARGE_PERSON);
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {

      case BEGINREG_DATE:
        dialog = BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(beginoccurrenceTime));
        break;
      case ENDREG_DATE:
        dialog = BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(endoccurrenceTime));
        break;
      case EVENT_CATEGORY:
        dialog = new EventCategoryDialogFragment();
        break;
      default:
        break;
    }
    FragmentManager fm = getFragmentManager();
    dialog.setTargetFragment(CompanyEventsQueryFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == BEGINREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      beginoccurrenceTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvBeginoccurrenceTime.setText(beginoccurrenceTime);
    }
    if (requestCode == ENDREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      endoccurrenceTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvEndoccurrenceTime.setText(endoccurrenceTime);
    }
    if (requestCode == EVENT_CATEGORY) {
      //大事类别
      CustomerDialog content =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      eventCategoryCode = content.getId();
      eventCategoryName = content.getName();
      tvEventCategory.setText(eventCategoryName);
    }
    if (requestCode == CHARGE_PERSON) {
      //负责人
      CustomerDialog person =
          (CustomerDialog) data.getSerializableExtra(SearchPersonListFragment.EXTRA_VALUE);
      chargeName = person.getName();
      chargeCode = person.getJobNo();
      tvChargePerson.setText(chargeName);
    }
  }

  public static CompanyEventsQueryFragment newInstance(ExtraValueContent queryConten) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryConten);
    CompanyEventsQueryFragment fragment = new CompanyEventsQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
